import MapLinking from '../utils/MapLinking'
import getlocation from '@react-native-community/geolocation';
import Layer from '../utils/Layer';

let objData = {}
let Data = {}
export function handleChange(flag) {
  getlocation.getCurrentPosition(
    (position) => {
      let longitude = position.coords.longitude
      let latitude = position.coords.latitude
      let key = '5005ff22b0ab9ca7697e71842d285878'
      // 高德逆地理编码，根据经纬度转换为地名
      fetch(`https://restapi.amap.com/v3/assistant/coordinate/convert?locations=${longitude},${latitude}&coordsys=gps&output=json&key=${key}`, { method: "GET" })
        .then(response => response.json())
        .then((jsonDa) => {
          let newVar = jsonDa.locations.split(',')
          longitude = newVar[0]//经度
          latitude = newVar[1]//纬度
          fetch(`http://restapi.amap.com/v3/geocode/regeo?key=${key}&location=${longitude},${latitude}&radius=1000&extensions=all&batch=false&roadlevel=0`, {
            method: "POST",
            headers: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            body: ``
          })
            .then((response) => response.json())
            .then((jsonData) => {
              try {
                let positions = jsonData.regeocode.aois[0].name
                // console.log(jsonData.regeocode.aois[0].name);
                objData = {
                  longitude,
                  latitude,
                  positions
                }
                // 当前经纬度
                // console.log(longitude);
                // console.log(latitude);
                // console.log(positions);
                if (flag) {
                  // 起始位置
                  const destLocation = {
                    lng: objData.longitude,
                    lat: objData.latitude,
                    title: objData.positions
                  };

                  // 结尾
                  const startLocation = {
                    lng: objData.longitude,
                    lat: objData.latitude,
                    title: objData.positions
                  };

                  MapLinking.planRoute({ startLocation, destLocation });
                }
              } catch (e) {
                // console.log(e);
              }
            })
            .catch((error) => {
              console.error(error);
            });
        })
    },
    (error) => {
      switch (error.code) {
        case 1:
          Layer.showMsg('定位失败')
          break;
        case 2:
          Layer.showMsg('定位失败，请查看手机是否开启GPS定位服务')
          break;
        case 3:
          Layer.showMsg('定位超时，请尝试重新获取定位')
          break;
        case 4:
          Layer.showMsg('定位失败')
          break;

        default:
          break;
      }
    },
    {
      // 超时时间
      timeout: 5000,
      maximumAge: 0
    }
  )
}
export async function GetLocation() {
  getlocation.getCurrentPosition(
    (position) => {
      let longitude = position.coords.longitude
      let latitude = position.coords.latitude
      let key = '5005ff22b0ab9ca7697e71842d285878'
      // 通过高德api转换精准经纬度
      fetch(`https://restapi.amap.com/v3/assistant/coordinate/convert?locations=${longitude},${latitude}&coordsys=gps&output=json&key=${key}`, { method: "GET" })
        .then(response => response.json())
        .then((jsonDa) => {
          let newVar = jsonDa.locations.split(',')
          longitude = newVar[0]//经度
          latitude = newVar[1]//纬度
          // 获取地名
          fetch(`http://restapi.amap.com/v3/geocode/regeo?key=${key}&location=${longitude},${latitude}&radius=1000&extensions=all&batch=false&roadlevel=0`, {
            method: "POST",
            headers: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            body: ``
          })
            .then((response) => response.json())
            .then((jsonData) => {
              try {
                let positions = jsonData.regeocode
                Data = {
                  longitude,
                  latitude,
                  positions
                }
                return Data
                // 当前经纬度
                // console.log(longitude);
                // console.log(latitude);
                // console.log(positions);
              } catch (e) {
                // console.log(e);
              }
              return Data
            })
            .catch((error) => {
              console.error(error);
            });
        })

    },
    (error) => {
      switch (error.code) {
        case 1:
          // Layer.showMsg('定位失败')
          break;
        case 2:
          Layer.showMsg('定位失败，请查看手机是否开启GPS定位服务')
          break;
        case 3:
          Layer.showMsg('定位超时，请尝试重新获取定位')
          break;
        case 4:
          Layer.showMsg('定位失败')
          break;

        default:
          break;
      }
    },
    {
      // 超时时间
      timeout: 5000,
      maximumAge: 0
    }
  )
  return Data
}
